package com.augurit.common.common.http.kt.api

import android.util.Log
import com.augurit.common.common.http.kt.model.Result
import com.augurit.common.common.http.kt.request.ExtraParam
import com.augurit.common.common.http.kt.request.IPlatFormStrategy
import com.augurit.common.common.http.kt.request.Method
import com.google.gson.reflect.TypeToken

/**
 *  author : yanghaozhang
 *  date : 2022/5/5 19:03
 *  description :
 */
object Api : TaskFactory.Convenience by ApiManager.instance {
    val log = true
    var requestCount = 0
    val inner = ApiManager.instance

    override fun <Response, OutputData> request(
        request: RequestConvertible<Response>,
        apiTransform: IApiTransform<Response, Result<OutputData, Exception>>
    ): IApiTask<Response, Result<OutputData, Exception>> {
        requestCount++
        if (log) {
            Log.d("Api:request:", "count = $requestCount")
        }
        return inner.request(request, apiTransform)
    }
}

inline fun <reified OutputData> IPlatFormStrategy.get(
    path: String,
    parameters: LinkedHashMap<String, String?>?,
    headers: Map<String, String>? = null,
): IApiTask<String, Result<OutputData, Exception>> {
    return Api.get(this, object : TypeToken<OutputData>() {}.type, path, parameters?.let { ExtraParam.PairParam(it) }, headers)
}

inline fun <reified OutputData> IPlatFormStrategy.post(
    path: String,
    parameters: LinkedHashMap<String,String?>?,
    headers: Map<String, String>? = null,
): IApiTask<String, Result<OutputData, Exception>> {
    return Api.post(this, object : TypeToken<OutputData>() {}.type, path, parameters?.let { ExtraParam.PairParam(it) }, headers)
}

inline fun <reified OutputData> IPlatFormStrategy.post(
    path: String,
    parameters: ExtraParam?,
    headers: Map<String, String>? = null,
): IApiTask<String, Result<OutputData, Exception>> {
    return Api.post(this, object : TypeToken<OutputData>() {}.type, path, parameters, headers)
}

inline fun <reified OutputData> IPlatFormStrategy.request(
    method: Method,
    path: String,
    parameters: ExtraParam?,
    headers: Map<String, String>? = null
): IApiTask<String, Result<OutputData, Exception>> {
    return Api.request(this, object : TypeToken<OutputData>() {}.type, method, path, parameters, headers)
}

inline fun <Response, reified OutputData> IPlatFormStrategy.request(
    noinline serviceBlock: suspend IPlatFormStrategy.() -> Response
): IApiTask<Response, Result<OutputData, Exception>> {
    return Api.request(this, serviceBlock, object : TypeToken<OutputData>() {}.type)
}

inline fun <reified OutputData> IPlatFormStrategy.requestString(
    noinline serviceBlock: suspend IPlatFormStrategy.() -> String
): IApiTask<String, Result<OutputData, Exception>> {
    return Api.request(this, serviceBlock, object : TypeToken<OutputData>() {}.type)
}

inline fun <reified OutputData> IPlatFormStrategy.requestBean(
    noinline serviceBlock: suspend IPlatFormStrategy.() -> OutputData
): IApiTask<OutputData, Result<OutputData, Exception>> {
    return Api.request(this, serviceBlock, object : TypeToken<OutputData>() {}.type)
}